package com.apop.common.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apop.common.exception.BizException;
import com.apop.common.mapper.OneMapper;
import com.apop.common.model.Result;
import com.apop.demo.encrypt.EncryptTypeEnum;
import com.apop.demo.encrypt.EncryptionStrategy;
import com.apop.demo.encrypt.EncryptionStrategyFactory;
import com.apop.demo.encrypt.pojo.DecodeDataDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @ClassName OneController
 * @author Jiayi Zheng
 * @date 2021/11/10 15:35
 * @Description: 测试
 * @version V1.0
 */
@RestController
@Slf4j
@Api(tags = "测试控制器")
@RequestMapping("/test")
public class TestController {

    @Autowired
    private OneMapper oneMapper;

    @Value("${mzt.redirect.url}")
    private  String redirectUrl ;

    @Value("${res.image.path}")
    private String path;

    @GetMapping("/image/{filename:.+}")
    public ResponseEntity<FileSystemResource> getImage(@PathVariable("filename") String filename) {
        File file = new File(path, filename);

        if (!file.exists()) {
            return ResponseEntity.notFound().build();
        }

        FileSystemResource resource = new FileSystemResource(file);
        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_JPEG) // 根据实际图片类型调整
                .body(resource);
    }

    /**
     * 测试
     * @return
     */
    @ApiOperation("业务异常demo接口")
    @GetMapping("/exception")
    @ResponseBody
    public Result test() {

        Map<String,Object> result = new HashMap();
        result.put("redirectUrl",redirectUrl);

        Boolean b = true;
        //业务异常测试
        if (b) {
            throw new BizException("-1", "业务异常！");
        }
        // 未知异常测试
        // int i = 1 / 0;

        return Result.success(result);
    }







    @PostMapping("/testOneMapper")
    @ResponseBody
    public Result EhcacheTest( HttpServletResponse response)  {
        String sql="SELECT * from T_PSM_MESSAGELOG";
        List<Map> list = oneMapper.executeQuery(sql);
        return Result.success(list);
    }

}
